#include "order/MultidatasAmend.h"
#include "tools/Diary.h"

MultidatasAmend::MultidatasAmend(QMap<QString, QMap<QString, QString>> &_RGLRLMAP_,
                                 QMap<ThreadPoint, NT::THREAD_STATE> &_pool_thread_,
                                 QMap<QString, bool> &_pool_bool_,
                                 QMap<QString, QString> &_pool_chars_,
                                 QMap<QString, QList<QString>> &_pool_ststr_,
                                 QMap<QString, NT::CUT_MEDIA_TYPE> &_pool_mtpltype_,
                                 QSqlDatabase *db){

    pool_thread = &_pool_thread_;
    pool_bool = &_pool_bool_;
    pool_chars = &_pool_chars_;
    pool_ststr = &_pool_ststr_;
    pool_mtpltype = &_pool_mtpltype_;
    Query = QSqlQuery(*db);
    DB = db;
    qRegisterMetaType<QList<int>>("QList<int>");
    _RGLRLMAP_["REGEXP"]["HyperLinkExtract"].isEmpty() ? HyperLink = "http[s]?://.*" : HyperLink = _RGLRLMAP_["REGEXP"]["HyperLinkExtract"];
}


void MultidatasAmend::run(){
    QMutex Lock;
    Lock.lock();
    int start = clock();
    (*pool_thread)[ThreadPoint::NETWORK_VISIT] = NT::THREAD_RECOVER;
    QStringList format_video = (*pool_ststr)["fm-video"];
    QStringList format_vorall = (*pool_ststr)["fm-ovall"];
    QStringList format_music;
    foreach(QString format, format_vorall) if (!format_video.contains(format)) format_music.append(format);
    NT::CUT_MEDIA_TYPE tbsw_type = static_cast<NT::CUT_MEDIA_TYPE>((*pool_mtpltype)["mdrs-table-type"]);
    QString mdrs_user_owner = (*pool_chars)["mdrs-owner"];
    int remment_data_row = 0;
    QList<int> otherness;
    QString media_tytuple;
    QString QuerySqlChar;
    QString joint = " and ";
    QMap<QString, QList<QString>> QueryData;
    if (NT::CUT_MUSIC_TYPE == tbsw_type)
    {
        media_tytuple = definfc.c_tuple(format_music);
    } else {
        media_tytuple = definfc.c_tuple(format_video);
    }
    QString SupermentsQuery = (*pool_chars)["mdrs-sql-filler"];
    QString condition1 = "owner like '%%1%'";
    QString condition2 = "mdrs_suffix in %1";
    QString condition3 = "mdrs_state=False ";
    if (!SupermentsQuery.isEmpty()) condition3 = condition3 + joint + SupermentsQuery;
    QStringList condition_totalist({condition1.arg(mdrs_user_owner),
                                    condition2.arg(media_tytuple),
                                    condition3});
    QuerySqlChar = condition_totalist.join(joint);
    QueryData = definfc.SuperQuery(DB, "media_datum", QuerySqlChar);
    QStringList MultiName = QueryData["mdrs_name"];
    QStringList MultiArtist = QueryData["mdrs_artist"];
    QStringList MultiPath = QueryData["mdrs_path"];
    QStringList MultiSuffix = QueryData["mdrs_suffix"];
    QStringList MultiSize = QueryData["mdrs_size"];
    QStringList MultiDurat = QueryData["mdrs_duration"];
    QStringList Multibitr = QueryData["mdrs_bitrate"];
    QStringList Multicover = QueryData["mdrs_cover"];
    QStringList MultiIsmove = QueryData["mdrs_state"];
    QStringList MultiProducTime = QueryData["prd_time"];
    QStringList MultiLyric = QueryData["mdrs_lyric"];
    QStringList MultiOwner = QueryData["owner"];
    QStringList reserveduty = (*pool_ststr)["reserve-duty-name"];
    (*pool_ststr)["reserve-duty-name"] = MultiName;
    (*pool_ststr)["reserve-duty-path"] = MultiPath;
    (*pool_ststr)["reserve-duty-artist"] = MultiArtist;
    (*pool_ststr)["reserve-duty-suffix"] = MultiSuffix;
    (*pool_ststr)["reserve-duty-bitrate"] = Multibitr;
    (*pool_ststr)["reserve-duty-cover"] = Multicover;
    (*pool_ststr)["reserve-duty-size"] = MultiSize;
    (*pool_ststr)["reserve-duty-duration"] = MultiDurat;
    (*pool_ststr)["reserve-duty-owner"] = MultiOwner;
    (*pool_ststr)["reserve-duty-lyric"] = MultiLyric;
    int real_max_row = MultiName.length();
    foreach(QString fillter, reserveduty)
    {
        if (MultiName.contains(fillter))
        {
            MultiName.removeOne(fillter);
            MultiArtist.removeOne(fillter);
            MultiSuffix.removeOne(fillter);
            Multibitr.removeOne(fillter);
            MultiSize.removeOne(fillter);
            MultiDurat.removeOne(fillter);
            remment_data_row++;
        }
        else
        {
            otherness.append(reserveduty.indexOf(fillter));
        }
    }
    diary6(" query data count: ", QString::number(MultiName.length()),
           " otherness count: ", QString::number(otherness.length()),
           " otherness rmv count: ", QString::number(remment_data_row), Diary::Debug)
    (*pool_bool)["otherness-finish"] = false;
    definfc.orderBy(otherness, true);
    emit amend_otherness_update(otherness);
    if (!otherness.isEmpty())
    {
        while (true) {
            if ((*pool_bool)["otherness-finish"])
            {
                break;
            }
            else
            {
                QThread::msleep(30);
            }
        }
    }
    emit amend_media_max(real_max_row);
    emit finish();
    int end = clock();
    bool coverf = !Extend::pcheck((*pool_ststr)["reserve-duty-cover"]).isEmpty();
    if (coverf) emit amend_cover_find();
    diary("amend data time ~comsuming: " + QString::number(end - start) + " cvfstat: " + QString::number(coverf), Diary::Debug)
    Lock.unlock();
    (*pool_thread)[ThreadPoint::NETWORK_VISIT] = NT::THREAD_IDLEABOUT;
}

